import React, {
    Component,
} from 'react';
import {
    StyleSheet,
    Text,
    View,
    Platform,
    Dimensions,
    TouchableOpacity,
    Image,
    Alert,
    FlatList,
    TextInput,
    Linking,
    ScrollView,
    Switch,
    DeviceEventEmitter,
} from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
import moment from 'moment';
import Loading from "../../utils/Loading";
import HttpUtils from '../../utils/HttpUtils';
import HttpUrls from '../../utils/HttpUrls';
import Header from "../../components/header";
import AndroidModule from '../../utils/AndroidConnent'
import Constant from '../../utils/Constant'//全局变量
import { ToastToUser } from '../../utils/CustomToast';
import CarImagesBox from '../car/carDetail/components/CarImagesBox';
import CarInfoBox from '../car/carDetail/components/CarInfoBox'
import CarReportBox from '../car/carDetail/components/CarReportBox'
import CarDealBox from '../car/carDetail/components/CarDealBox'
import CarPurchaseInfoBox from '../car/carDetail/components/CarPurchaseInfoBox'
import CarRecordBox from '../car/carDetail/components/CarRecordBox'
import CarConfigBox from '../car/carDetail/components/CarConfigBox'
import CarDescriptionBox from '../car/carDetail/components/CarDescriptionBox'
import CarCompanyBox from '../car/carDetail/components/CarCompanyBox'
import LogisticsBox from './orderComponents/LogisticsBox'
import { getImgUrl,getPerm } from '../../utils/ComFunction'
import BigImageContainer from '../../components/Tool/BigImageContainer';
import MoreFunctionPop from '../car/carDetail/components/MoreFunctionPop';
import AlertCommonView from '../../components/Tool/AlertCommonView';
import { CarSaleStatus, carDetectionStatus,PermissionType } from '../../utils/Enum'
import QuotationListModal from "../offerprice/QuotationListModal"
import LogisticsModal from './LogisticsModal'



let navigation
let mark = '_.'

const isIos = Platform.OS == 'ios';

export default class CarOfferPriceDetailPage extends Component {

    constructor(props) {
        super(props);
        let carId = this.props.route && this.props.route.params && this.props.route.params.carId ? this.props.route.params.carId : ''
        let merchantId = this.props.route && this.props.route.params && this.props.route.params.merchantId ? this.props.route.params.merchantId : ''
        let isJoinCar = this.props.route.params && this.props.route.params.isJoinCar ? this.props.route.params.isJoinCar : 0
        let priceOperatePerm = getPerm(PermissionType.Permission_EnableOfferPriceOperate)
        this.state = {
            isJoinCar,
            priceOperatePerm,
            merchantId: merchantId,
            isSelfCar: Constant.userInfo.merchantId == merchantId, //是否是自己的车
            isCollected: false, //是否收藏车辆
            saleStatus: '',
            carId: carId,
            carPics: [],
            data: {},
            detectionStatus: '', //检测状态
            quotationRecordStatus: '', //0未报价 1已报价 2报价已确认 3已下单 4驳回 5已作废
            isFranchiseeSendQuotation: false,
            quotationStatisticsData: {}, //报价统计信息
            quotationData: {},//报价单信息
            merchantData: {}, //商家信息
            reportPic: '',
            reportPcUrl: '',
            reportPdf: '',
            detectionReportPic: '',
            detectionReportUrl: '',
            detectionReportPdf: '',
            featuresArray: [],
            showBigImageContainer: false,
            currImgIndex: 0,
            showMorePop: false, //显示更多功能
            moreFunctionData: [
                { 'title': '刷新上架', key: 'upCar' },
                { 'title': '检测', key: 'detection' },
                { 'title': '编辑车辆', key: 'editCar' },
                { 'title': '定价', key: 'settingPrice' },
                { 'title': '报价记录', key: 'carOfferPriceHistory' },
                { 'title': '退库', key: 'cancelStock' },
            ],
            sellerMerchantId: '',
            isShowQuotationListModal: false,
            clickQuotationProgressStatus: 0,
            historyRO: {},
            showAlertData: {},
            showAlertCommonView: false,
            isShowLogisticsModal: false,
            currentLogisticsItem: {},

        }
    }

    componentWillUnmount() {
        if (this.offerPriceSuccessListener) {
            this.offerPriceSuccessListener.remove();
        }
    }

    componentDidMount() {
        this.offerPriceSuccessListener = DeviceEventEmitter.addListener('offerPriceStateChanged', e => {
            setTimeout(() => {
                this.getQuotationByCarId()
            }, 1000);
        });

        this.getCarDetails()
        if (!this.state.isSelfCar) {
            //获取收藏状态
            this.getCarCollectStatus()
            //获取买家用户报价单信息
            this.getQuotationByCarId()
            //获取商家信息
            this.getMerchantInformation()
        } else {
            //获取报价单的统计信息
           
        }
        this.getQuotationStatisticsByCarId()
    }

    getCarDetails() {
        let param = {
            carId: this.state.carId,
            merchantId: this.state.merchantId
        }
        // if (this.Loading) {
        //     this.Loading.show();
        // }

        let url = ''
        if (this.state.merchantId == Constant.userInfo.merchantId) {//是自己的车
            url = HttpUrls.getOwnCarDetails
        } else {//是别人的车
            param['clickMerchantId'] = Constant.userInfo.merchantId
            url = HttpUrls.getOthersCarDetails
        }


        HttpUtils.getRequest(url, param)
            .then((result) => {
                if (this.Loading) {
                    this.Loading.hide();
                }
                let data = result.data
                let configArray = []
                if (data.features && data.features != mark) {
                    let featuresArray = data.features.split(mark)
                    if (featuresArray.length > 0) {
                        let featuresFirst = featuresArray[0]
                        let featuresFirstArray = featuresFirst.split(',')
                        featuresFirstArray.map((item) => {
                            configArray.push(item)
                        })
                        if (featuresArray.length > 1) {
                            configArray.push(featuresArray[1])
                        }
                    }
                }

                let carPics = []
                if (data.carPics) {
                    data.carPics.map((item) => {
                        carPics.push({ url: getImgUrl(item.picUrl) })
                    })
                }

                this.setState({
                    data,
                    detectionStatus: data.detectionStatus ? data.detectionStatus : '',
                    saleStatus: data.saleStatus,
                    carPics: carPics,
                    reportPic: data.maintenanceRecord && data.maintenanceRecord.reportPic ? data.maintenanceRecord.reportPic : '',
                    reportPcUrl: data.maintenanceRecord && data.maintenanceRecord.reportPcUrl ? data.maintenanceRecord.reportPcUrl : '',
                    reportPdf: data.maintenanceRecord && data.maintenanceRecord.reportPdf ? data.maintenanceRecord.reportPdf : '',
                    detectionReportPic: data.detectionReportPic ? data.detectionReportPic : '',
                    detectionReportUrl: data.detectionReportUrl ? data.detectionReportUrl : '',
                    detectionReportPdf: data.detectionReportPdf ? data.detectionReportPdf : '',
                    featuresArray: configArray,
                    sellerMerchantId: data.merchantId
                }, function () {

                })
            })
            .catch((error) => {
                if (this.Loading) {
                    this.Loading.hide();
                }
            });
    }

    getCarCollectStatus() {
        let param = {
            goodsId: this.state.carId,
            type: 2,
            userId: Constant.userInfo.userId
        }
        HttpUtils.postRequest(HttpUrls.checkCollected, param)
            .then((result) => {
                this.setState({
                    isCollected: result.data ? result.data : false
                })
            })
            .catch((error) => {

            });
    }

    getMerchantInformation() {
        HttpUtils.getRequest(HttpUrls.getMerchantInformation, { merchantId: this.state.merchantId })
            .then((result) => {
                if (result.code == 0) {
                    this.setState({
                        merchantData: result.data,
                    })
                }
            })
    }

    getQuotationStatisticsByCarId() {
        
        let param = {
            carId: this.state.carId
        }
        HttpUtils.getRequest(HttpUrls.getQuotationStatisticsByCarId, param)
            .then((result) => {
                if (result.code == 0) {
                    let data = result.data
                    this.setState({
                        quotationStatisticsData: data,
                    })
                }
            })
            .catch((error) => {

            });
    }

    getQuotationByCarId() {
        let param = {
            carId: this.state.carId
        }
        HttpUtils.getRequest(HttpUrls.getQuotationByCarId, param)
            .then((result) => {
                console.log('getQuotationByCarId', result.data.quotationRecordStatus)
                if (result.code == 0) {
                    let data = result.data
                    if (result.data) {
                        this.setState({
                            quotationData: data,
                            quotationRecordStatus: data.quotationRecordStatus != undefined ? data.quotationRecordStatus : '',
                            historyRO: data.historyRO
                        })

                    } else {

                        this.setState({

                            quotationRecordStatus: '',

                        })
                    }


                }
            })
            .catch((error) => {

            });
    }

    applyDetection() {
        if (this.state.quotationData.applyDetection == 1) {
            ToastToUser('你已申请过检测')
            return
        }
        let param = {
            carId: this.state.carId,
            buyerUserId: Constant.userInfo.userId,
            buyerMerchantId: Constant.userInfo.merchantId,
            sellerMerchantId: this.state.merchantId
        }
        if (this.Loading) {
            this.Loading.show('申请检测中...')
        }
        HttpUtils.postRequest(HttpUrls.applyDetection, param)
            .then((result) => {
                if (this.Loading) {
                    this.Loading.hide()
                }
                if (result.code == 0) {

                    navigation.navigate('MessageChat', {
                        imGroupId: result.data.imGroupId,
                        carId: this.state.carId
                    })
                    this.getQuotationByCarId()
                    // navigation.navigate('CarOperateSuccessPage', {
                    //     operateTitle: '检测申请提交成功！', completion: () => {
                    //         this.getQuotationByCarId()
                    //     }
                    // })
                } else {
                    ToastToUser(result.msg ? result.msg : result.statusText)
                }
            })
            .catch((error) => {
                if (this.Loading) {
                    this.Loading.hide()
                }
            });
    }

    upCar() {
        let param = {
            carId: this.state.carId,
            operator: Constant.userInfo.fullname,
            merchantId: Constant.userInfo.merchantId,
            joinQuotationCenter: 1
        }

        this.Loading.show('正在刷新上架...')
        HttpUtils.postRequest(HttpUrls.refreshUpperTime, param)
            .then((result) => {
                this.Loading.hide()
                if (result.code == 0) {
                    ToastToUser('刷新成功')
                } else {
                    ToastToUser(result.msg ? result.msg : result.statusText)
                }
            })
            .catch((error) => {
                this.Loading.hide()
            });

    }

    render() {
        navigation = this.props.navigation;
        return (
            <View style={styles.container}>
                <Header navigation={navigation} Title={'报价详情'} RightButton={() => this.renderRightButton()}></Header>
                <ScrollView
                    style={{ flex: 1 }}
                    showsVerticalScrollIndicator={false}
                >
                    <CarImagesBox
                        carPics={this.state.carPics}
                        morePicHandler={(index) => {
                            this.setState({
                                currImgIndex: index,
                                showBigImageContainer: true
                            })
                        }}
                    />

                    {/* 车辆信息 */}
                    {
                        this.state.data && Object.keys(this.state.data).length > 0 ?
                            <CarInfoBox
                                quotationRecordStatus={this.state.quotationRecordStatus}
                                quotationStatisticsData={this.state.quotationStatisticsData}
                                isFranchisee={true}
                                isSelfCar={this.state.isSelfCar}
                                isPriceOperatePerm={this.state.priceOperatePerm ? true : false}
                                data={this.state.data}
                                isOfferPrice={true}
                            />
                            :
                            null
                    }

                    {/* 车辆档案 */}
                    <CarRecordBox
                        data={this.state.data}
                        lookRecordDetail={() => {
                            navigation.navigate('CarRecordPage', { data: this.state.data })
                        }}
                    />

                    {/* 维保、检测报告 */}
                    <CarReportBox
                        carId={this.state.carId}
                        vcode={this.state.data.vcode}
                        navigation={navigation}
                        reportPic={this.state.reportPic}
                        reportPcUrl={this.state.reportPcUrl}
                        reportPdf={this.state.reportPdf}
                        detectionReportPic={this.state.detectionReportPic}
                        detectionReportUrl={this.state.detectionReportUrl}
                        detectionReportPdf={this.state.detectionReportPdf}
                    />

                    {/* 收购信息 */}
                    {
                        this.state.isSelfCar ?
                            <CarPurchaseInfoBox
                                data={this.state.data}
                            />
                            :
                            null
                    }

                    {/* 物流信息 */}
                    {
                        !Constant.userInfo.isFranchisee ?
                            <LogisticsBox
                                sellerMerchantId={this.state.merchantId}
                                showModal={(item)=>{
                                    this.setState({
                                        currentLogisticsItem: item,
                                        isShowLogisticsModal: true
                                    })
                                }}
                            />
                            :
                            null
                    }

                    {/* 成交信息 */}
                    {
                        this.state.saleStatus == CarSaleStatus.CarSaleStatusSaled ?
                            <CarDealBox
                                data={this.state.data}
                            />
                            :
                            null
                    }

                    {/* 车辆配置 */}
                    <CarConfigBox
                        data={this.state.featuresArray}
                        lookCarConfigDetail={() => {
                            navigation.navigate('CarConfigPage', { species: this.state.data.species })
                        }}
                    />

                    {/* 车况描述 */}
                    {
                        this.state.data && this.state.data.carCondition ?
                            <CarDescriptionBox
                                name={'车况描述'}
                                carCondition={this.state.data.carCondition}
                            />
                            :
                            null
                    }

                    {/* 瑕疵描述 */}
                    {
                        this.state.data && this.state.data.faultDescribe && this.state.isSelfCar ?
                            <CarDescriptionBox
                                name={'瑕疵描述'}
                                carCondition={this.state.data.faultDescribe}
                            />
                            :
                            null
                    }

                    {/* 公司信息 */}
                    {
                        !this.state.isSelfCar && Object.keys(this.state.merchantData).length > 0 ?
                            <CarCompanyBox
                                navigation={navigation}
                                data={this.state.merchantData}
                                isJoinCar={this.state.isJoinCar}
                            />
                            :
                            null
                    }

                    <View style={{ height: 10 }} />

                </ScrollView>

                {/* 底部按钮 */}
                {
                    this.state.isSelfCar ?
                        this.renderFranchiseeBottomBox()
                        :
                        this.renderBottomBox()
                }


                <Loading onRef={(ref) => (this.Loading = ref)} />

                {
                    this.state.showBigImageContainer ?
                        <BigImageContainer
                            idx={this.state.currImgIndex}
                            images={this.state.carPics}
                            closeHandler={(index) => {
                                this.setState({
                                    currImgIndex: index,
                                    showBigImageContainer: false
                                })
                            }}
                        />
                        :
                        null
                }

                {
                    this.state.showMorePop ?
                        <MoreFunctionPop
                            visible={this.state.showMorePop}
                            data={this.state.moreFunctionData}
                            clickHandle={(item, index) => {
                                this.setState({
                                    showMorePop: false
                                }, function () {
                                    this.moreFuction(item)
                                })
                            }}
                            cancelHandle={() => {
                                this.setState({
                                    showMorePop: false
                                })
                            }}
                        />
                        :
                        null
                }

                {this.state.isShowQuotationListModal ?
                    <QuotationListModal
                        navigation={navigation}
                        visible={this.state.isShowQuotationListModal}
                        onClose={() => this.setModalVisible(false)}
                        carId={this.state.carId}
                        quotationProgressStatus={this.state.clickQuotationProgressStatus}
                    />
                    :
                    null
                }

                {this.state.isShowLogisticsModal ?
                    <LogisticsModal
                        visible={this.state.isShowLogisticsModal}
                        onClose={() => {
                            this.setState({
                                isShowLogisticsModal: false
                            })
                        }}
                        item={this.state.currentLogisticsItem}
                        quotationProgressStatus={this.state.clickQuotationProgressStatus}
                    />
                    :
                    null
                }


                {
                    this.state.showAlertCommonView ?
                        <AlertCommonView
                            title={this.state.showAlertData.title}
                            content={this.state.showAlertData.content}
                            cancelBtnText={this.state.showAlertData.cancelBtnText}
                            sureBtnText={this.state.showAlertData.sureBtnText}
                            cancelHandler={() => {
                                this.setState({
                                    showAlertCommonView: false
                                })
                            }}
                            sureHandler={() => {
                                this.setState({
                                    showAlertCommonView: false
                                })
                                if (this.state.showAlertData.type == 'upCar') {
                                    this.upCar()
                                }
                            }}
                        />
                        :
                        null
                }



            </View>
        )
    }

    setModalVisible(visible) {
        this.setState({
            isShowQuotationListModal: visible
        })
    }

    renderFranchiseeBottomBox() {
        return (
            <View style={[styles.bottomBg, { height: 50 + Constant.bottomHeight }]}>
                <View style={styles.bottomBox}>
                    <View style={styles.bottomLeftBox}>
                        <TouchableOpacity
                            style={styles.itemBox}
                            activeOpacity={0.8}
                            onPress={() => {
                                this.setState({
                                    isShowQuotationListModal: true,
                                    carId: this.state.carId,
                                    clickQuotationProgressStatus: 2
                                })
                            }}
                        >
                            <Image style={styles.itemImg} source={require('source/image/chat_orange_icon.png')} />
                            <Text style={styles.itemText}>沟通中</Text>
                        </TouchableOpacity>
                        <TouchableOpacity
                            style={styles.itemBox}
                            activeOpacity={0.8}
                            onPress={() => {
                                this.setState({
                                    isShowQuotationListModal: true,
                                    carId: this.state.carId,
                                    clickQuotationProgressStatus: 4
                                })
                            }}
                        >
                            <Image style={styles.itemImg} source={require('source/image/wait_orange_icon.png')} />
                            <Text style={styles.itemText}>待确认</Text>
                        </TouchableOpacity>
                    </View>

                    <View style={styles.bottomRightBox}>
                        <TouchableOpacity
                            activeOpacity={0.8}
                            onPress={() => {
                                this.setState({
                                    showMorePop: true
                                })
                            }}
                        >
                            <LinearGradient start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }}
                                colors={['#FFEF3A', '#FFDB1B']}
                                style={styles.bottomRightItemBox}>
                                <Text style={styles.bottomRightItemText}>车辆操作</Text>

                            </LinearGradient>
                        </TouchableOpacity>
                    </View>

                </View>
            </View>
        )
    }

    renderBottomBox() {
        // 加盟商非在库车源进来的不能报价
        if(this.state.isJoinCar == 1){
            return null
        }
        return (
            <View style={[styles.bottomBg, { height: 50 + Constant.bottomHeight }]}>
                <View style={styles.bottomBox}>
                    <View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'center', marginRight: 8 }}>
                        <TouchableOpacity
                            style={styles.itemBox}
                            activeOpacity={0.8}
                            onPress={() => {
                                this.consultOnline()
                            }}
                        >
                            <Image style={styles.itemImg} source={require('source/image/chat_online_icon.png')} />
                            <Text style={styles.itemText}>在线咨询</Text>
                        </TouchableOpacity>

                        <TouchableOpacity
                            style={styles.itemBox}
                            activeOpacity={0.8}
                            onPress={() => {
                                this.callTel(this.state.carId)
                            }}
                        >
                            <Image style={styles.itemImg} source={require('source/image/offerPrice/phone.png')} />
                            <Text style={styles.itemText}>电话咨询</Text>
                        </TouchableOpacity>

                        {this.state.quotationRecordStatus == 2 && this.state.priceOperatePerm ?
                            <TouchableOpacity
                                style={styles.itemBox}
                                activeOpacity={0.8}
                                onPress={() => {
                                    if (this.Loading) {
                                        this.Loading.show();
                                    }
                            
                                    this.quotationReject()
                                }}
                            >
                                <Image style={styles.itemImg} source={require('source/image/cancel_order_icon.png')} />
                                <Text style={styles.itemText}>取消订单</Text>
                            </TouchableOpacity>
                            : null
                        }

                    </View>


                    <View style={styles.bottomRightBox}>
                        {
                            //已报价或者报价已确认
                            this.state.quotationRecordStatus == 1 || this.state.quotationRecordStatus == 2 || (this.state.historyRO && this.state.historyRO.isReject == '1' &&
                                this.state.quotationRecordStatus == 0)?
                                <View style={{ flexDirection: 'column', alignItems: 'flex-end', justifyContent: 'center', marginRight: 8 }}>
                                    <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                                        <Text style={{ fontSize: 20, fontWeight: '500', color: '#ED542E' }}>{(this.state.quotationData.quotationPrice / (10000 * 100)).toFixed(2)}</Text>
                                        <Text style={{ fontSize: 12, color: '#ED542E', marginLeft: 2, marginTop: 5 }}>万元</Text>
                                    </View>
                                    <Text style={{ fontSize: 12, color: '#333', marginTop: 2 }}>{this.state.historyRO.quotationMerchantId == Constant.userInfo.merchantId ? '我的报价' : '对方报价'}</Text>
                                </View>
                                :
                                null
                        }

                        {this.getBottomBtn()}

                        {/* {
                            //车辆还没检测成功
                            this.state.detectionStatus != carDetectionStatus.carDetectionStatusDetected ?
                                <TouchableOpacity
                                    activeOpacity={0.8}
                                    onPress={() => {
                                        this.applyDetection()
                                    }}
                                >
                                    <LinearGradient start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }}
                                        colors={this.state.quotationData.applyDetection ? ['#F5F5F5', '#F5F5F5'] : ['#FFEF3A', '#FFDB1B']}
                                        style={styles.bottomRightItemBox}>
                                        <Text style={styles.bottomRightItemText}>{this.state.quotationData.applyDetection ? '已申请' : '申请检测'} </Text>

                                    </LinearGradient>
                                </TouchableOpacity>
                                :
                                // 车辆已检测
                                this.state.detectionStatus == carDetectionStatus.carDetectionStatusDetected ? this.getBottomBtn()
                                    :
                                    null
                        } */}
                    </View>

                </View>
            </View>
        )
    }

    getBottomBtn() {
        if(!this.state.priceOperatePerm){
            return  null
        }
        if (this.state.quotationRecordStatus == 0 || this.state.quotationRecordStatus == 4 || this.state.quotationRecordStatus == '') {// 未报价(显示立即报价)
            
            if(this.state.historyRO && this.state.historyRO.isReject == '1'){
                return (
                    <TouchableOpacity
                        activeOpacity={0.8}
                        onPress={() => {
                            navigation.navigate('CarOfferPricePage', { carId: this.state.carId, merchantId: this.state.merchantId, isEdit: true })
                        }}
                    >
                        <LinearGradient start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }}
                            colors={['#FFEF3A', '#FFDB1B']}
                            style={styles.bottomRightItemBox}>
                            <Text style={styles.bottomRightItemText}>{'修改报价'} </Text>
    
                        </LinearGradient>
                    </TouchableOpacity>
                )
            }else{
                return (
                    <TouchableOpacity
                        activeOpacity={0.8}
                        onPress={() => {
                            navigation.navigate('CarOfferPricePage', { carId: this.state.carId, merchantId: this.state.merchantId, isEdit: false })
                        }}
                    >
                        <LinearGradient start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }}
                            colors={['#FFEF3A', '#FFDB1B']}
                            style={styles.bottomRightItemBox}>
                            <Text style={styles.bottomRightItemText}>{'立即报价'} </Text>
    
                        </LinearGradient>
                    </TouchableOpacity>
                )
            }
            
        } else if (this.state.quotationRecordStatus == 1) {// 已报价(显示待确认)
            if (this.state.historyRO.isReject == '1') {//报价被驳回(显示修改报价)
                return (
                    <TouchableOpacity
                        activeOpacity={0.8}
                        onPress={() => {
                            navigation.navigate('CarOfferPricePage', { carId: this.state.carId, merchantId: this.state.merchantId, isEdit: true })
                        }}
                    >
                        <LinearGradient start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }}
                            colors={['#FFEF3A', '#FFDB1B']}
                            style={styles.bottomRightItemBox}>
                            <Text style={styles.bottomRightItemText}>{'修改报价'} </Text>
                        </LinearGradient>
                    </TouchableOpacity>
                )
            } else {
                return (
                    <TouchableOpacity
                        activeOpacity={0.8}
                        onPress={() => {
                            
                            this.quotationReject()
                        }}
                    >
                        <LinearGradient start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }}
                            colors={['#F5F5F5', '#F5F5F5']}
                            style={styles.bottomRightItemBox}>
                            <Text style={styles.bottomRightItemText}>{'取消报价'} </Text>

                        </LinearGradient>
                    </TouchableOpacity>
                )
            }

        } else if (this.state.quotationRecordStatus == 2) {
            return (
                <TouchableOpacity
                    activeOpacity={0.8}
                    onPress={() => {
                        navigation.navigate('BuyerStartOrderPage', { carId: this.state.carId, merchantId: this.state.quotationData.buyerMerchantId })
                    }}
                >
                    <LinearGradient start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }}
                        colors={['#FFEF3A', '#FFDB1B']}
                        style={styles.bottomRightItemBox}>
                        <Text style={styles.bottomRightItemText}>{'确认订单'} </Text>

                    </LinearGradient>
                </TouchableOpacity>
            )
        }
        // else if (this.state.detectionStatus != carDetectionStatus.carDetectionStatusDetected) {
        //     return (
        //         <TouchableOpacity
        //             activeOpacity={0.8}
        //             onPress={() => {
        //                 this.applyDetection()
        //             }}
        //         >
        //             <LinearGradient start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }}
        //                 colors={this.state.quotationData.applyDetection ? ['#F5F5F5', '#F5F5F5'] : ['#FFEF3A', '#FFDB1B']}
        //                 style={styles.bottomRightItemBox}>
        //                 <Text style={styles.bottomRightItemText}>{this.state.quotationData.applyDetection ? '已申请' : '申请检测'} </Text>

        //             </LinearGradient>
        //         </TouchableOpacity>
        //     )
        // }
    }

    quotationReject() {
        let params = {
            quotationRecordId: this.state.quotationData.quotationRecordId,
            operator: Constant.userInfo.fullname
        }
        HttpUtils.postRequest(HttpUrls.quotationCancel, params).then(result => {

            if (result.code == 0) {
                ToastToUser('取消成功')
                this.getQuotationByCarId()
            } else {
                ToastToUser(result.msg ? result.msg : result.statusText)
            }
        }).catch(err => {

        })
    }


    consultOnline() {
        let param = {
            carId: this.state.carId,
            buyerMerchantId: Constant.userInfo.merchantId,
            sellerMerchantId: this.state.sellerMerchantId,
            buyerUserId: Constant.userInfo.userId,
        }
        if (this.Loading) {
            this.Loading.show();
        }

        HttpUtils.postRequest(HttpUrls.quotationConsult, param)
            .then((result) => {
                if (this.Loading) {
                    this.Loading.hide();
                }
                if (result.code == 0) {

                    setTimeout(() => {
                        navigation.navigate('MessageChat', {
                            imGroupId: result.data.imGroupId,
                            carId: this.state.carId
                        })
                    }, 500);

                } else {
                    ToastToUser(result.msg + '')
                }
            })
            .catch((error) => {
                if (this.Loading) {
                    this.Loading.hide();
                }
            });
    }

    callTel(carId) {
        let param = {
            carId: carId,
        }
        HttpUtils.getRequest(HttpUrls.getCarSellerContactUserInfo, param)
            .then((result) => {
                console.log(result, 'res')
                if (result.success) {
                    console.log(result.data.sellerUserPhone, 'phone')
                    const url = `tel:${result.data.sellerUserPhone}`;
                    Linking.canOpenURL(url)
                        .then(supported => {
                            if (supported) {
                                return Linking.openURL(url);
                            } else {
                                Alert.alert('提示', '该设备不支持拨打电话');
                            }
                        })
                        .catch(err => {
                            console.error('拨号失败:', err);
                            Alert.alert('错误', '无法拨打电话');
                        });
                } else {
                    ToastToUser(result.msg)
                }
            })
            .catch((error) => {

            });
    }


    renderRightButton() {
        return (
            <View style={styles.rightBg}>
                {
                    !this.state.isSelfCar && Object.keys(this.state.data).length > 0 ?
                        <TouchableOpacity
                            activeOpacity={0.8}
                            style={styles.rightItemBox}
                            onPress={() => { this.collection() }}>
                            <Image source={this.state.isCollected ? require('source/image/collection_sel_icon.png') : require('source/image/collection_icon.png')} style={styles.rightImg} />
                        </TouchableOpacity>
                        :
                        null
                }
                {/* <TouchableOpacity
                    activeOpacity={0.8}
                    style={styles.rightItemBox}
                    onPress={() => { this.share() }}>
                    <Image source={require('source/image/share_icon.png')} style={styles.rightImg} />
                </TouchableOpacity> */}
            </View>
        )
    }

    collection() {
        let param = {
            goodsId: this.state.carId,
            type: 2,
            userId: Constant.userInfo.userId
        }
        HttpUtils.postRequest(this.state.isCollected ? HttpUrls.cancleCollection : HttpUrls.addCollection, param)
            .then((result) => {
                if (result.data) {
                    let isCollected = this.state.isCollected
                    this.setState({
                        isCollected: !isCollected
                    }, function () {
                        ToastToUser(this.state.isCollected ? '收藏成功!' : '取消收藏成功!')
                    })
                }

            })
            .catch((error) => {

            });
    }

    share() {

    }

    moreFuction(item) {
        if (item.key == 'upCar') {//刷新上架
            this.setState({
                showAlertData: {
                    title: '刷新上架',
                    content: '是否确认刷新上架？',
                    cancelBtnText: '取消',
                    sureBtnText: '确定',
                    type: 'upCar'
                },
                showMorePop: false,
                showAlertCommonView: true
            })
        } else if (item.key == 'detection') {// 检测
            if (!this.state.data.vcode) {
                ToastToUser('请先填写车辆VIN码')
                return
            }
            if (this.state.data.detectionStatus == 1) {
                ToastToUser('车辆正在等待检测中，请勿重复提交')
                return
            }
            navigation.navigate('CarDetectionPage', {
                carId: this.state.carId, completion: () => {
                    this.getCarDetails()
                }
            })
        } else if (item.key == 'editCar') {//编辑车辆
            navigation.navigate('PublishCarPage', {
                carId: this.state.carId, completion: () => {
                    navigation.goBack()
                    this.getCarDetails()
                }
            })
        } else if (item.key == 'settingPrice') {//定价
            navigation.navigate('CarPriceSettingPage', {
                carId: this.state.carId, completion: () => {
                    navigation.goBack()
                    this.getCarDetails()
                }
            })
        } else if (item.key == 'carOfferPriceHistory') {//报价记录
            navigation.navigate('CarOfferPriceHistoryPage', { carId: this.state.carId })
        } else if (item.key == 'cancelStock') {//退库
            navigation.navigate('CarCancelStockPage', {
                carId: this.state.carId, completion: () => {
                    this.getCarDetails()
                }
            })
        }
    }

}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F9F9F9',
    },
    rightBg: {
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'flex-end',
        marginRight: 8,
    },
    rightItemBox: {
        height: '100%',
        width: 40,
        justifyContent: 'center',
        alignItems: 'center'
    },
    rightImg: {
        width: 24,
        height: 24
    },
    sepLine: {
        width: '100%',
        height: 10
    },
    bottomBg: {
        flexDirection: 'row',
        width: '100%',
        backgroundColor: '#fff',
        borderTopColor: '#E4E4E4',
        borderTopWidth: 0.5
    },
    bottomBox: {
        width: '100%',
        paddingHorizontal: 16,
        height: 50,
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between',
    },
    itemBox: {
        height: '100%',
        width: 45,
        marginRight: 5,
        flexDirection: 'column',
        justifyContent: 'center',
        alignItems: 'center',
    },
    itemImg: {
        width: 24,
        height: 24
    },
    itemText: {
        color: '#333333',
        fontSize: 10,
        marginTop: 4,
    },
    bottomLeftBox: {
        height: '100%',
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'flex-start',
    },
    bottomRightBox: {
        height: '100%',
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'flex-start',
    },
    bottomRightItemBox: {
        width: 140,
        height: 36,
        borderRadius: 18,
        justifyContent: 'center',
        alignItems: 'center',
    },
    bottomRightItemText: {
        color: '#666666',
        fontSize: 14,
        fontWeight: '500'
    },
})